//
//  DevFormSelectorField.swift
//  DevFormView
//
//  Created by dev on 15/9/18.
//  Copyright (c) 2015年 devilved. All rights reserved.
//

import UIKit

class DevFormSelectorField: DevFormField {
    
    var hiddenTextField:UITextField?
    
    private var valueTextLabel:UILabel?
    
    private var arrowImageView:UIImageView?
    
    var textFont:UIFont?
    
    var keyboardType:UIKeyboardType?
    
    var inputView:UIView?
    
    var returnKeyType:UIReturnKeyType?
    
    var getTextFromValue:(AnyObject?) -> String? = {(value:AnyObject?) -> String? in return value as? String}
    
    override func initDevFormFieldView() {
        super.initDevFormFieldView()
        self.hiddenTextField = UITextField()
        let text:UITextField = self.hiddenTextField!
        let main:UIView = self.view!
        let width:CGFloat = self.formTableView.frame.width
        let left:CGFloat = self.textLabel!.frame.origin.x + self.textLabel!.frame.width
        let right:CGFloat = self.formTableView.separatorInset.right > 0 ? self.formTableView.separatorInset.right : 15
        
        text.delegate = self.formTableView
        text.text = self.defaultValue as? String
        text.frame = CGRectMake(left, (self.height - self.controlHeight) / 2, width - left - right, self.controlHeight)
        if let keyboardType = self.keyboardType {
            text.keyboardType = keyboardType
        }
        
        if let inputView = self.inputView {
            text.inputView = inputView
        }
        
        if let returnKeyType = self.returnKeyType {
            text.returnKeyType = returnKeyType
        }
        
        main.addSubview(text)
        
        text.hidden = true
        
        text.addTarget(self, action: "onEditingDidBegin", forControlEvents: UIControlEvents.EditingDidBegin)
        text.addTarget(self, action: "onEditingDidEnd", forControlEvents: UIControlEvents.EditingDidEnd)
        
        self.valueTextLabel = UILabel()
        let valueText = self.valueTextLabel!
        if let font:UIFont = self.textFont {
            valueText.font = font
        }
        let arrowWidth:CGFloat = valueText.font.pointSize
        let arrowHeight:CGFloat = valueText.font.pointSize
        
        valueText.textColor = UIColor.lightGrayColor()
        valueText.textAlignment = NSTextAlignment.Right
        valueText.frame = CGRectMake(left, (self.height - self.controlHeight) / 2, width - left - right - arrowWidth + 3, self.controlHeight)
        valueText.text = self.getTextFromValue(self.defaultValue)
        main.addSubview(valueText)
        
        self.arrowImageView = UIImageView()
        let arrow:UIImageView = self.arrowImageView!
        arrow.image = UIImage(named: "dev-right-arrow")
        arrow.frame = CGRectMake(formTableView.frame.width - right - arrowWidth + 3, (self.height - arrowHeight) / 2, arrowWidth, arrowHeight)
        main.addSubview(arrow)
        
        
    }
    
    override func initConstraints() {
        let text:UITextField = self.hiddenTextField!
        let main:UIView = self.view!
        let valueText = self.valueTextLabel!
        let arrow:UIImageView = self.arrowImageView!
//        let left:CGFloat = self.textLabel!.frame.origin.x + self.textLabel!.frame.width
        let right:CGFloat = self.formTableView.separatorInset.right > 0 ? self.formTableView.separatorInset.right : 15
        let arrowWidth:CGFloat = valueText.font.pointSize
        let arrowHeight:CGFloat = valueText.font.pointSize
        
        self.hiddenTextField?.translatesAutoresizingMaskIntoConstraints = false
        valueText.translatesAutoresizingMaskIntoConstraints = false
        arrow.translatesAutoresizingMaskIntoConstraints = false
        
        main.addConstraint(NSLayoutConstraint(item: main, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: text, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: right))
        
        main.addConstraint(NSLayoutConstraint(item: text, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self.textLabel!, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        
        main.addConstraint(NSLayoutConstraint(item: text, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: main, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        main.addConstraint(NSLayoutConstraint(item: valueText, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self.textLabel!, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        
        main.addConstraint(NSLayoutConstraint(item: valueText, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: main, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        main.addConstraint(NSLayoutConstraint(item: arrow, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: main, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        main.addConstraint(NSLayoutConstraint(item: arrow, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: arrowWidth))
        
        main.addConstraint(NSLayoutConstraint(item: arrow, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: arrowHeight))
        
        main.addConstraint(NSLayoutConstraint(item: main, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: arrow, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: right - 3))
        
        main.addConstraint(NSLayoutConstraint(item: valueText, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: arrow, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
    }
    
    override func setFieldValue(value: AnyObject?) {
        super.setFieldValue(value)
        self.hiddenTextField?.text = value as? String
        self.valueTextLabel?.text = self.getTextFromValue(value)
    }
    
    override func click() {
        self.focus()
    }
    
    override func focus() {
        if self.hiddenTextField!.canBecomeFirstResponder() {
            self.hiddenTextField!.becomeFirstResponder()
        }
        super.focus()
    }
    
    func getText() -> String? {
        return self.getTextFromValue(self.getFieldValue())
    }
    
    func onEditingDidBegin() {
        super.focus()
    }
    
    func onEditingDidEnd() {
        self.setFieldValue(self.hiddenTextField?.text)
    }
    
    override func getEditableView() -> UIView? {
        return self.hiddenTextField
    }
}
